﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using HZ.MBSM.DBUtility;
public partial class Bidder_Acceptbidder : System.Web.UI.Page
{
    public Int32 PJID;
    Int64 AccountId_TBR;
    string accountid_sc;
  
    protected void Page_Load(object sender, EventArgs e)
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
         System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
        
        if (_Account != null)
        {
            if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
            {
                System.Data.IDataReader dr;
                AccountId_TBR = SysFun.ToLong(_Account.AccountId);
                PJID = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["ProjectId"]));
                accountid_sc = SysFun.IsSQL(Request.QueryString["SC"]);

                string sqlstr = " select count(1) from bid_goods_tbr where accountid_sc=" + accountid_sc + "and accountid_tb =" + AccountId_TBR.ToString() + " and projectid=" + PJID.ToString();
                Db db = new Db();
                DataTable gg = db.DataTable(sqlstr);
                if (gg != null && SysFun.ToInt( gg.Rows[0][0])<1)
                {
                    Response.Write("<script>alert('非法操作！');window.opener.location.reload();window.close();</script>");
                }                 
                db.Close();
                gg.Dispose(); 
                showGrid();
            }

        }
        else
        {
            Response.Write("<script>window.alert('对不起，请重新登录！');</script>");
            Response.Write("<script>window.location.href='../HomePage/Default.aspx';</script>");
        }

    }
    protected void showGrid()
    {
        string strConditions;
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "v_product_goods as a inner join bid_Goods_TBR  as b on a.goodsid=b.goodsid  inner join bid_project_product d on a.productid=d.productid ";                                               //要查询的表明
        //要查询的表明
        dbPage.Key = "a.goodsid";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "a.goodsid,a.productname,a.Medicinemodel,a.Outlookc,a.Unit,a.xishu,a.usercode as usercode_SC, a.companyname as username_SC,b.Accept_time, case status when 0 then '未确认' when 1 then '已确认'  when 2 then '已撤销' else '未设置' end as status";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 15;                                                    //没有显示记录条数   
        dbPage.OrderBy = "a.goodsid,Productname";        //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键
       
    }
    public string fctConditions()
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        string strwhere = "  b.projectid=" + PJID.ToString() + " and b.AccountId_TB='" + _Account.AccountId + "' and a.AccountId_SC=" + accountid_sc;
        return strwhere;

    }

    protected void Button1_Click(object sender, EventArgs e)
    {            
        
        SetStatus(1);

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //SetStatus(2);//撤销
    }

    protected void SetStatus (int status)
    {
        string strSql = "";
        string goodsid = "";
        goodsid = SysFun.ToTrim(Request.Form["chk"]);
       
        bool succ = false;
        Db db = new Db();
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (goodsid != "")
        {
            string[] SGID = goodsid.Length == 0 ? new string[] { "" } : goodsid.Substring(0).Split(new char[] { ',' });
            strSql = "";
            for (int i = 0; i < SGID.Length; i++)
            {
                string[] goodsids = SGID[i].ToString().Length == 0 ? new string[] { "" } : SGID[i].ToString().Substring(0).Split(new char[] { '@' });
                strSql += "@" + "update bid_goods_tbr set status=" + status.ToString() + " ,Accept_time =getdate() where goodsid='" + goodsids[0].ToString() + "' and projectid=" + PJID.ToString();

                if (status == 1 && goodsids[1].ToString()!="已确认")
                {
                    strSql += "@" + "insert into Project_Bid(accountid,projectid,goodsid,checkstatus) VALUES (" + _Account.AccountId.ToString() + "," + PJID.ToString() + "," + goodsids[0].ToString() + ",1)";

                }
            
                if (status == 2 && goodsids[1].ToString() != "已撤销")
                {
                    strSql += "@" + "delete from  Project_Bid where  goodsid =" + goodsids[0].ToString();

                }
               

            
            }
            if (strSql.Length > 0)
            {
                succ=db.ExecuteTransaction(strSql.Substring(1).Split(new char[] { '@' }), 30);
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "投标人确认/撤销" + status.ToString(), "bid_goods_tbr", strSql, "../Bidder/Acceptbidder.aspx");
            }
        }

        db.Close();
        if (succ)
        {
            Response.Write("<script>alert('状态更新成功');window.opener.location.reload();window.close();</script>");
        }
        else
        {
            Response.Write("<script>alert('操作失败,请确认您是否已经执行!');window.opener.location.reload();</script>");
        }
       


        
    }
}
